﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using CarInfor.DAL;
using System.Data;

namespace CarInfor.BLL
{
    /// <summary>
    /// 获取机动车年检时间
    /// </summary>
    public class YearCheckSplit
    {
        /// <summary>
        /// 代码用途：
        /// 1.系统启动的时候，扫描年检表
        /// 2.到期日期+年检推后日期小于当前日期  则将年检日期改为到期日期，自动计算到期日期并赋值
        /// 3.当用打开年检资料编辑的时候，根据选择的年检日期自动计算到期日期，保存的时候，自动更新到数据库
        /// </summary>
        /// <param name="yearCheck">年检资料</param>
        /// <returns>到期月份数</returns>
        public static int GetYearSplit(YearCheck yearCheck)
        {
            decimal result = 1;
            if (!string.IsNullOrEmpty(yearCheck.BaoHuDate))
            {
                //保户日期距离今天年限
                decimal splitYear = (decimal)(DateTime.Now - Convert.ToDateTime(yearCheck.BaoHuDate)).TotalDays / 365;
                string filename = System.Windows.Forms.Application.StartupPath + "\\CarTypeSplitConfig.xml";
                XDocument xml = XDocument.Load(filename);
                XElement carTypeElement = xml.Root.Elements().Where(o => o.Attribute("Name").Value == yearCheck.CarType).FirstOrDefault();
                if (carTypeElement != null)
                {
                    //报废车辆不能再进行年检
                    if (splitYear > int.Parse(carTypeElement.Attribute("DelayYearLimit").Value))
                    {
                        return -1;
                    }
                    //如果限制使用日期和延期日期相同，则表示每年年检一次
                    if (int.Parse(carTypeElement.Attribute("YearLimit").Value) == int.Parse(carTypeElement.Attribute("DelayYearLimit").Value))
                    {
                        return 12;
                    }
                    //如果在使用年限范围内，则每年检测一次,否则按照延期使用年检规定执行
                    if (splitYear > int.Parse(carTypeElement.Attribute("YearLimit").Value))
                    {
                        XElement xeSplit = carTypeElement.Elements().Where(o => decimal.Parse(o.Attribute("Min").Value) < splitYear && splitYear < decimal.Parse(o.Attribute("Max").Value)).FirstOrDefault();
                        if (xeSplit != null)
                        {
                            result = decimal.Parse(xeSplit.Element("Split").Value);
                        }
                    }
                }
            }
            return Convert.ToInt32(12 / result);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        public static void UpdateYearCheckData()
        {
            AlertSetting als = new AlertSetting();
            als.GetSetting();
            CarInfor.DAL.YearCheck dalYearCheck = new YearCheck();
            DataTable dt = dalYearCheck.Query(" ExpireDate < #" + DateTime.Now.AddDays(als.YearCheckAlertE * (-1)) + "# ");
            if (dt != null && dt.Rows.Count > 0)
            {
                //需要处理的年检数据列表
                List<YearCheck> lst = dt.Rows.Cast<DataRow>().Select(e => new YearCheck()
                {
                    ID = e["编号"].ToString(),
                    CarNumber = e["车辆号牌"].ToString(),
                    CarType = e["车辆类型"].ToString(),
                    OwnerName = e["车主姓名"].ToString(),
                    Telephone = e["联系电话"].ToString(),
                    CheckDate = e["年检日期"].ToString(),
                    ExpireDate = e["到期日期"].ToString(),
                    BaoHuDate = e["报户日期"].ToString(),
                    Memo = e["备注"].ToString()

                }).ToList();

                foreach (var item in lst)
                {
                    int sp = YearCheckSplit.GetYearSplit(item);

                    item.CheckDate = Convert.ToDateTime(item.ExpireDate).ToShortDateString();
                    if (sp > 0)
                    {
                        item.ExpireDate = Convert.ToDateTime(item.CheckDate).AddMonths(YearCheckSplit.GetYearSplit(item)).Date.ToShortDateString();
                    }
                    else
                    {
                        item.ExpireDate = Convert.ToDateTime("8888-8-8 12:00:00").ToString();
                    }
                    item.UpdateExpireDate(item.ID);
                }
                //递归
                YearCheckSplit.UpdateYearCheckData();
            }
            else
            {
                return;
            }
        }
    }
}
